package com.yn.xgame.logic.handler;

import com.yn.xgame.DBHandler;
import com.yn.xgame.XAssert;
import com.yn.xgame.logic.GamePlayer;
import com.yn.xgame.logic.player.*;
import com.yn.xgame.message.C2GPbRedirectMessage;
import com.yn.xgame.message.UserSessionMessage;
import org.apache.commons.dbutils.QueryRunner;
import org.slf4j.Logger;

import java.sql.Connection;
import java.util.List;
import java.util.Map;

public class DbLoadHandler extends DBHandler {

	public DbLoadHandler(QueryRunner runner, Connection conn) {
		m_runner = runner;
		m_conn = conn;
	}

	@Override
	public Logger logger() {

		return null;
	}
	
	public GamePlayer loadUser() {
		return loadUser();
	}
	
	public UserSessionMessage<GamePlayer> loadUser(C2GPbRedirectMessage req) {
		return loadUser(req.getFromGateTopic(), req.getSessionId(), req.getPb().getLogin().getUsername());
	}
	
	public UserSessionMessage<GamePlayer> loadRegistUser(C2GPbRedirectMessage req) {
		return loadUser(req.getFromGateTopic(), req.getSessionId(), req.getPb().getRegist().getUsername());
	}
	
	public UserSessionMessage<GamePlayer> loadUser(String fromGateTopic, long sid, String username) {
		int userId;
		String sql = "select * from user where user_name = ?";
		GamePlayer player = new GamePlayer(sid);
		try {
			Map<String, Object> userMap = queryOne(sql, new Object[] { username });
			XAssert.nonNull(userMap.get("user_id"),userMap);
			XAssert.nonNull(userMap.get("user_code"),userMap);
			XAssert.nonNull(userMap.get("user_nickname"),userMap);
			XAssert.nonNull(userMap.get("user_pwd"),userMap);
			//XAssert.nonNull(userMap.get("user_bank_pwd"),userMap);
			XAssert.nonNull(userMap.get("user_horn"),userMap);
			XAssert.nonNull(userMap.get("user_icon"),userMap);
			XAssert.nonNull(userMap.get("user_level"),userMap);
			XAssert.nonNull(userMap.get("user_vip_level"),userMap);
			XAssert.nonNull(userMap.get("user_exp"),userMap);
			XAssert.nonNull(userMap.get("user_bank_gold"),userMap);
			XAssert.nonNull(userMap.get("user_wallet_gold"),userMap);
			
			
			player.setUserId((Integer) userMap.get("user_id"));
			player.setUserCode((Integer) userMap.get("user_code"));
			player.setUserNickName((String) userMap.get("user_nickname"));
			player.setPwd((String) userMap.get("user_pwd"));
			player.setBankPwd((String) userMap.get("user_bank_pwd")!=null?(String) userMap.get("user_bank_pwd"):null);
			player.setUserHorn((Integer) userMap.get("user_horn"));
			player.setUserIcon((Integer) userMap.get("user_icon"));
			player.setUserLevel((Integer) userMap.get("user_level"));
			player.setUserVipLevel((Integer) userMap.get("user_vip_level"));
			player.setUserExp((Integer) userMap.get("user_exp"));
			player.setUserBankGold((String) userMap.get("user_bank_gold"));
			player.setUserWalletGold((String) userMap.get("user_wallet_gold"));
			player.setUserTel((String) userMap.get("user_tel"));
			player.setUserName((String) userMap.get("user_name"));
			player.setUserStatus((String) userMap.get("user_status"));
			userId=(int) userMap.get("user_id");
			
			//背包
			String sql2="select * from user_cash_coupon where user_id=?";
            List<BackPack> userBackPack = queryList(sql2, BackPack.class, new Object[]{userId});
            BackPackModule backPackModule= new BackPackModule(player, userBackPack);
            player.setBackPackModule(backPackModule);
            //用户信息
            String sql3="select * from user_message where user_id=? and (msg_isread=0 or msg_isread=1)";
            List<UserMessage> userMessages= queryList(sql3, UserMessage.class,new Object[]{userId});
            UserMessageModule userMessageModule= new UserMessageModule(player,userMessages);
            player.setUserMessageModule(userMessageModule);
            //
            String sql4="select * from user_assets_log where user_id=?";
            List<UserBankAssetsLog> userBankAssetsLogs= queryList(sql4, UserBankAssetsLog.class,new Object[]{userId});
            UserBankModule userBankModule= new UserBankModule(player, userBankAssetsLogs);
            player.setUserBankModule(userBankModule);
		} catch (Exception e) {
			logger().error("load exception: ", e);
			return null;
		}
		return new UserSessionMessage<>(fromGateTopic, sid, player);

	}

}
